using System;
using System.Collections;
using System.Windows.Forms;
using ODX.Core;

namespace AddressBook
{
    public interface IEntityForm
    {
        Entity Object { get; set; }
    }

    public class ListViewCRUD
    {
        private ListView list;
        public ListViewCRUD(ListView list, IEnumerable listSource)
        {
            this.list = list;
            list.ItemActivate += List_OnItemActivate;

            foreach (Entity e in listSource)
                list.Items.Add(CreateItem(list, e));
        }

        private void List_OnItemActivate(object sender, EventArgs e)
        {
            Edit();

            //Session.CurrentSession.SavePoint();
            //if (form.ShowDialog() != DialogResult.Cancel)
            //{
            //    Session.CurrentSession.DeletePoint();
            //    UpdateItem(list.FocusedItem);
            //}
            //else
            //    Session.CurrentSession.RestorePoint();
        }

        private Type formType;
        public Type FormType
        {
            get { return formType; }
            set { formType = value; }
        }

        private ListViewItem CreateItem(ListView list, Entity e)
        {
            ArrayList texts = new ArrayList();
            foreach (ColumnHeader header in list.Columns)
            {
                string path = (string)header.Text;
                texts.Add(e.GetType().GetProperty(path).GetValue(e, null).ToString());
            }

            ListViewItem item = new ListViewItem((string[])texts.ToArray(typeof(string)));
            item.Tag = e;

            return item;
        }

        private void UpdateItem(ListViewItem listViewItem)
        {
            ArrayList texts = new ArrayList();
            Entity e = listViewItem.Tag as Entity;
            foreach (ColumnHeader header in list.Columns)
            {
                string path = (string)header.Text;
                texts.Add(e.GetType().GetProperty(path).GetValue(e, null).ToString());
            }

            //listViewItem.Text = texts[0] as string;
            for (int i = 0; i < texts.Count; i++)
                listViewItem.SubItems[i].Text = texts[i] as string;
        }

        public void New(Entity newEntity)
        {
            Form form = (Form)Activator.CreateInstance(formType);
            if (form is IEntityForm)
                ((IEntityForm)form).Object = newEntity;

            if (form.ShowDialog() != DialogResult.Cancel)
                list.Items.Add(CreateItem(list, newEntity));
            else
                newEntity.Delete();
        }

        public void Edit()
        {
            Form form = (Form)Activator.CreateInstance(formType);
            if (form is IEntityForm)
                ((IEntityForm)form).Object = list.FocusedItem.Tag as Entity;

            if ( form.ShowDialog() == DialogResult.OK )
                UpdateItem(list.FocusedItem);
        }

        public void Delete()
        {
            Entity e = list.FocusedItem.Tag as Entity;
            e.Delete();
            list.Items.Remove(list.FocusedItem);
        }
    }
}
